var TokenManager = function () { }
TokenManager.prototype.test = function () { },

    TokenManager.prototype.operationRobot = function () {
        window.BizFunc.resetActionArr();
        window.BizFunc.resetRobotByStageConfig();
        window.BizFunc.resetSomeDynamicData();
        window.BizFunc.resetLandByStageConfig();

        this.switchToken(window.GameViewData.dynamicData.tokenArr);
        if (window.GameViewData.actionArr.length <= 1) {
            console.log("window.GameViewData.actionArr.length <= 1");
            return;
        }
        var seq = cc.sequence(window.GameViewData.actionArr);
        window.GameViewData.robot.node.runAction(seq);

    },

    TokenManager.prototype.advance = function () {
        switch (window.GameViewData.robot.dir) {
            case window.GameEnum.DirEnum.DIR_UP:
                if (window.GameViewData.robot.localY <= 0) {
                    console.log("window.GameViewData.robot.localY <= 0 ");
                    break;
                }
                window.GameViewData.robot.localY--;
                break;
            case window.GameEnum.DirEnum.DIR_DOWN:
                if (window.GameViewData.robot.localX + window.GameViewData.curStage.rowNum * (window.GameViewData.robot.localY + 1) >= window.GameViewData.curStage.allNum) {
                    console.log("robot.y超出下边界了");
                    break;
                }
                window.GameViewData.robot.localY++;
                break;
            case window.GameEnum.DirEnum.DIR_LEFT:

                if (window.GameViewData.robot.localX <= 0) {
                    console.log("window.GameViewData.robot.localX <= 0 ");
                    break;
                }
                window.GameViewData.robot.localX--;
                break;
            case window.GameEnum.DirEnum.DIR_RIGHT:
                if ((window.GameViewData.robot.localX + 1) + window.GameViewData.curStage.rowNum * window.GameViewData.robot.localY >= window.GameViewData.curStage.allNum
                    || window.GameViewData.robot.localX >= window.GameViewData.curStage.rowNum - 1
                ) {
                    console.log("robot.x超出右边界了");
                    break;
                }
                window.GameViewData.robot.localX++;
                break;
            default:
                break;
        }

        window.BizFunc.updatePointByLocal();

    },

    TokenManager.prototype.turnLeft = function () {
        var count = window.GameViewData.robot.dir - window.GameEnum.DirEnum.DIR_DEFAULT;
        var modNum = count % window.GameEnum.DirEnum.DIR_COUNT;
        modNum++;
        window.GameViewData.robot.dir = modNum + window.GameEnum.DirEnum.DIR_DEFAULT;

        var action = cc.rotateBy(0.5, -90);
        // var action = cc.moveTo(0.5, window.GameViewData.robot.pointX, window.GameViewData.robot.pointY);
        window.GameViewData.actionArr.push(action);

    },

    TokenManager.prototype.turnRight = function () {
        var count = window.GameViewData.robot.dir - window.GameEnum.DirEnum.DIR_DEFAULT;
        count += window.GameEnum.DirEnum.DIR_COUNT * window.GameEnum.DirEnum.DIR_COUNT;
        count--;
        var modNum = count % window.GameEnum.DirEnum.DIR_COUNT;
        if (modNum == 0) {
            modNum += window.GameEnum.DirEnum.DIR_COUNT;
        }
        window.GameViewData.robot.dir = modNum + window.GameEnum.DirEnum.DIR_DEFAULT;
        var action = cc.rotateBy(0.5, 90);
        window.GameViewData.actionArr.push(action);
    },
    TokenManager.prototype.leap = function () {
        this.advance();
        var action = cc.jumpTo(0.5, window.GameViewData.robot.pointX, window.GameViewData.robot.pointY, 50, 4);
        window.GameViewData.actionArr.push(action);
        window.GameViewData.robot.localZ++;
    },
    TokenManager.prototype.pick = function () {
        var pointIndex = window.GameViewData.curStage.rowNum * window.GameViewData.robot.localY + window.GameViewData.robot.localX;

        if (window.GameViewData.curStage.landArr[pointIndex].type == 1) {
            if (window.GameViewData.curStage.landArr[pointIndex].state == 0) {
                window.GameViewData.curStage.landArr[pointIndex].changeAppearance();
                window.GameViewData.curStage.landArr[pointIndex].state = 1;
                window.GameViewData.dynamicData.target++;
            } else {
                window.GameViewData.curStage.landArr[pointIndex].resetAppearance();
                window.GameViewData.curStage.landArr[pointIndex].state = 0;
                window.GameViewData.dynamicData.target--;
            }
        }

        if (window.GameViewData.dynamicData.target == window.GameViewData.curStage.target) {
            window.GameViewData.dynamicData.completed = true;
            var finished = cc.callFunc(function () {
                window.GameViewData.dynamicData.result = "完美";
                window.EventBus.pos(window.GameConstant.EventTypeID.OnResultChange);
            }, this, 0);
            window.GameViewData.actionArr.push(finished);
        }
    },

    TokenManager.prototype.switchToken = function (arr) {
        for (var i = 0; i < arr.length; i++) {
            switch (arr[i].type) {
                case window.GameEnum.TokenTypeEnum.TOKEN_ADVANCE:
                    window.TokenManager.advance();
                    // this.advance();
                    var action = cc.moveTo(0.5, window.GameViewData.robot.pointX, window.GameViewData.robot.pointY);
                    window.GameViewData.actionArr.push(action);
                    break;
                case window.GameEnum.TokenTypeEnum.TOKEN_PICK:
                    this.pick();
                    break;
                case window.GameEnum.TokenTypeEnum.TOKEN_THROW:
                    console.log("TOKEN_THROW");
                    break;
                case window.GameEnum.TokenTypeEnum.TOKEN_TURN_LEFT:
                    this.turnLeft();
                    break;
                case window.GameEnum.TokenTypeEnum.TOKEN_TURN_RIGHT:
                    this.turnRight();
                    break;
                case window.GameEnum.TokenTypeEnum.TOKEN_LEAP:
                    this.leap();
                    break;
                case window.GameEnum.TokenTypeEnum.TOKEN_FUNC:
                    this.func1();
                    break;
                case window.GameEnum.TokenTypeEnum.TOKEN_FUNC2:
                    this.func2();
                    break;
                default:
                    console.log("default");
                    break;
            }

            // 动作栈深度加1，超过阈值则返回
            window.GameViewData.dynamicData.curStack++;
            // 是否已经完成关卡
            if (window.GameViewData.dynamicData.completed) {
                break;
            }
            if (window.GameViewData.dynamicData.curStack > window.GameConstant.GameDefaultValue.STACK_ACTION_MAX) {
                break;
            }
        }
    },

    TokenManager.prototype.func1 = function () {
        this.switchToken(window.GameViewData.dynamicData.func1TokenArr);
    },    
    TokenManager.prototype.func2 = function () {
        this.switchToken(window.GameViewData.dynamicData.func2TokenArr);
    },

    TokenManager.prototype.addToken = function (touchLayout, tokenItem, index) {
        switch (touchLayout) {
            case window.GameConstant.TouchLayout.LAYOUT_MAIN:
                this.addMainToken(tokenItem, index);
                break;
            case window.GameConstant.TouchLayout.LAYOUT_FUNC1:
                this.addFunc1Token(tokenItem, index);
                break;
            case window.GameConstant.TouchLayout.LAYOUT_FUNC2:
                this.addFunc2Token(tokenItem, index);
                break;
            default:
                console.log("addToken can not find touchLayout");
                break;
        }
    },

    TokenManager.prototype.removeToken = function (touchLayout, index) {
        console.log("removeToken");
        switch (touchLayout) {
            case window.GameConstant.TouchLayout.LAYOUT_MAIN:
                this.removeMainToken(index);
                break;
            case window.GameConstant.TouchLayout.LAYOUT_FUNC1:
                this.removeFunc1Token(index);
                break;
            case window.GameConstant.TouchLayout.LAYOUT_FUNC2:
                this.removeFunc2Token(index);
                break;
            default:
                console.log("removeToken can not find touchLayout");
                break;
        }
    },

    TokenManager.prototype.addMainToken = function (tokenItem, index) {
        this.addCommonToken(window.GameViewData.dynamicData.tokenArr, tokenItem, index);
        window.GameViewData.dynamicData.arrChange = true;
    },

    TokenManager.prototype.removeMainToken = function (index) {
        this.removeCommonToken(window.GameViewData.dynamicData.tokenArr, index);
        window.GameViewData.dynamicData.arrChange = true;
    },
    TokenManager.prototype.addFunc1Token = function (tokenItem, index) {
        this.addCommonToken(window.GameViewData.dynamicData.func1TokenArr, tokenItem, index);
        window.GameViewData.dynamicData.func1ArrChange = true;
    },

    TokenManager.prototype.removeFunc1Token = function (index) {
        this.removeCommonToken(window.GameViewData.dynamicData.func1TokenArr, index);
        window.GameViewData.dynamicData.func1ArrChange = true;
    },
    TokenManager.prototype.addFunc2Token = function (tokenItem, index) {
        this.addCommonToken(window.GameViewData.dynamicData.func2TokenArr, tokenItem, index);
        window.GameViewData.dynamicData.func2ArrChange = true;
    },

    TokenManager.prototype.removeFunc2Token = function (index) {
        this.removeCommonToken(window.GameViewData.dynamicData.func2TokenArr, index);
        window.GameViewData.dynamicData.func2ArrChange = true;
    },

    TokenManager.prototype.addCommonToken = function (arr, tokenItem, index) {
        // 克隆了一个节点
        var tokenItemNode = cc.instantiate(tokenItem);
        var tempNode = cc.instantiate(tokenItem.node);

        tokenItemNode.node = tempNode;
        tokenItemNode.type = tokenItem.type;
        tokenItemNode.value = tokenItem.value;

        // 插入或者添加
        if (index > -1) {
            // arrayObject.splice(index,howmany,item1,.....,itemX)
            // index	必需。整数，规定添加/删除项目的位置，使用负数可从数组结尾处规定位置。
            // howmany	必需。要删除的项目数量。如果设置为 0，则不会删除项目。
            // item1, …, itemX	可选。向数组添加的新项目。
            arr.splice(index, 0, tokenItemNode);
        } else {
            arr.push(tokenItemNode);
        }
    },

    TokenManager.prototype.removeCommonToken = function (arr, index) {
        arr.splice(index, 1);
    },

    module.exports = TokenManager; 